package org.robot.scm.platform.wxrobot.processor;

import java.util.List;
import java.util.Objects;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.robot.scm.platform.AbstractMonitor;
import org.robot.scm.platform.monitor.FinNewNoticeMonitor;
import org.robot.scm.platform.wxrobot.AbstractRobotProcessor;
import org.robot.scm.pojos.TargetNotify;
import org.robot.scm.pojos.config.BaseConfig;
import org.robot.scm.pojos.config.base.BasePlatform;
import org.robot.scm.pojos.enums.PlatformEnum;
import org.robot.scm.pojos.platform.FinNewsBaseNotify;
import org.robot.scm.pojos.platform.MessageBaseNotify;
import org.robot.scm.retry.SimpleRetryable;
import org.robot.scm.utils.StreamUtils;

/**
 * 财经新闻
 *
 * @Author : Awn create on 2024/4/22 15:52
 */
@Slf4j
public class FiNewsRobotProcessor extends AbstractRobotProcessor {

    @Override
    public List<String> getKeywords(BaseConfig config) {
        return config.get().getPlatform(PlatformEnum.FINNEWS).getKeywords();
    }

    @Override
    protected List<TargetNotify> process(BaseConfig config, String sendTo, String msg) {
        return new SimpleRetryable(3, 2000).retry(() -> this.doProcess(sendTo), Objects::nonNull);
    }

    private List<TargetNotify> doProcess(String sendTo) {
        BasePlatform currentPlatform = AbstractMonitor.mainConfig.getPlatform(PlatformEnum.FINNEWS);
        BaseConfig baseConfig = new BaseConfig(AbstractMonitor.mainConfig, currentPlatform);
        FinNewNoticeMonitor finNewNoticeMonitor = (FinNewNoticeMonitor) StreamUtils.filterOne(AbstractMonitor.getMonitors(), e -> e.getPlatform().equals(PlatformEnum.FINNEWS));
        FinNewsBaseNotify notify = (FinNewsBaseNotify) finNewNoticeMonitor.read(baseConfig);
        MessageBaseNotify finNewsBean = MessageBaseNotify.builder().message(notify.getMessage()).build();
        List<String> filter = StreamUtils.filter(baseConfig.get().getRobots(PlatformEnum.FINNEWS), e -> StringUtils.equals(e, sendTo));
        return StreamUtils.convertAndDistinct(filter, e -> finNewsBean.robotToNotifyBean(e, baseConfig));
    }
}
